package com.ruoyi.biz.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import cn.licoy.encryptbody.annotation.encrypt.EncryptBody;
import cn.licoy.encryptbody.enums.EncryptBodyMethod;
import com.ruoyi.common.utils.excel.EasyExcelUtil;
import com.ruoyi.common.utils.excel.ExcelListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.biz.domain.TscSignal;
import com.ruoyi.biz.service.ITscSignalService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 交通信号灯设备Controller
 *
 * @author ruoyi
 * @date 2021-04-26
 */
@EncryptBody(value = EncryptBodyMethod.AES)
@Api(value = "交通信号灯设备API", tags = {"交通信号灯设备"})
@RestController
@RequestMapping("/tsc/signal")
public class TscSignalController extends BaseController {
    @Autowired
    private ITscSignalService tscSignalService;

    /**
     * 查询交通信号灯设备列表
     */
    @ApiOperation("查询交通信号灯设备列表")
    @PreAuthorize("@ss.hasPermi('tsc:signal:list')")
    @GetMapping("/list")
    public TableDataInfo list(TscSignal tscSignal) {
        startPage();
        List<TscSignal> list = tscSignalService.queryList(tscSignal);
        return getDataTable(list);
    }

    /**
     * 导出交通信号灯设备列表到Excel
     */
    @ApiOperation("导出交通信号灯设备列表到Excel")
    @PreAuthorize("@ss.hasPermi('tsc:signal:export')")
    @Log(title = "交通信号灯设备", businessType = BusinessType.EXPORT)
    @GetMapping("/exportExcel")
    public AjaxResult exportExcel(TscSignal tscSignal) {
        List<TscSignal> list = tscSignalService.queryList(tscSignal);
        //获取模型类简称
        String simpleName = TscSignal.class.getSimpleName();
        String absoluteFile = EasyExcelUtil.getAbsoluteFile("交通信号灯设备");
        EasyExcelUtil.write(absoluteFile, TscSignal.class, list, 1, simpleName);
        return AjaxResult.success("交通信号灯设备");
    }

    /**
     * 导入Excel到交通信号灯设备列表
     */
    @ApiOperation("导入Excel到交通信号灯设备列表")
    @PreAuthorize("@ss.hasPermi('tsc:signal:import')")
    @Log(title = "交通信号灯设备", businessType = BusinessType.IMPORT)
    @GetMapping("/importExcel")
    public AjaxResult importExcel(MultipartFile multipartFile) throws IOException {
        //文件流
        InputStream inputStream = multipartFile.getInputStream();
        //异步读取excl文件
        EasyExcelUtil.asyncReadModel(inputStream,new ExcelListener(), TscSignal.class,0,1);
        // 解析每行结果在listener中处理
        ExcelListener<TscSignal> excelListener = new ExcelListener<TscSignal>();
        //获取监听器解析数据
        List<TscSignal> rows = excelListener.getRows();
        //批量插入
        return AjaxResult.success(tscSignalService.saveBatch(rows));
    }

    /**
     * 获取交通信号灯设备详情
     */
    @ApiOperation("获取交通信号灯设备详情")
    @PreAuthorize("@ss.hasPermi('tsc:signal:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tscSignalService.getById(id));
    }

    /**
     * 新增交通信号灯设备
     */
    @ApiOperation("新增交通信号灯设备")
    @PreAuthorize("@ss.hasPermi('tsc:signal:add')")
    @Log(title = "交通信号灯设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TscSignal tscSignal) {
        return toAjax(tscSignalService.save(tscSignal));
    }

    /**
     * 修改交通信号灯设备
     */
    @ApiOperation("修改交通信号灯设备")
    @PreAuthorize("@ss.hasPermi('tsc:signal:edit')")
    @Log(title = "交通信号灯设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TscSignal tscSignal) {
        return toAjax(tscSignalService.updateById(tscSignal));
    }

    /**
     * 删除交通信号灯设备
     */
    @ApiOperation("删除交通信号灯设备")
    @PreAuthorize("@ss.hasPermi('tsc:signal:remove')")
    @Log(title = "交通信号灯设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tscSignalService.removeByIds(Arrays.asList(ids)));
    }
}
